/*
 * @Author: Creator
 * @Date: 2020-10-05 00:26:29
 * @LastEditors: Creator
 * @LastEditTime: 2020-10-09 15:29:48
 * @Description: file content
 */
#include "hospital.h"
#include "string.h"
#include <stdlib.h>
#include "global.h"
#include "tools.h"
extern Date LastUpdateTime;
extern Date NowTime;
extern float HospitalIncome;
//extern const Nature RoomTotal = 50;
extern Nature RoomNow;
extern HsptRecNode *HsptRecList;
extern Nature PatientInHsptNum;


HsptRecNode* createNullHsptRecNode()
{
    HsptRecNode *temp = (HsptRecNode*)malloc(sizeof(HsptRecNode));
    temp->pre = temp->next = NULL;
    return temp;
}

HsptRecNode* readAHsptRecNode(FILE* fp)
{
    if(!fp)
    {
        printf("ERROR:Fail to read file!\n");
        return 0;
    }

    HsptRecNode* temp = createNullHsptRecNode();

    fscanf(fp,"%s",temp->data.patientid);       //读取id
    temp->data.in = readTime(fp);               // 读取住院时间
    temp->data.preout = readTime(fp);           // 读取出院时间
    fscanf(fp,"%f",&temp->data.deposit);        // 读取当前剩余押金
    return temp;    
}

HsptRecNode* loadHsptRecNodeList()
{
    FILE* fp = NULL;
    if((fp = fopen("hospital.json","r")) == NULL)
    {
        printf("ERROR:Can't open patient database!\n");
        return 0;
    }
    fscanf(fp,"%d",&PatientInHsptNum);      // 读取患者数
    HsptRecNode* HsptRecodeList = NULL;     // 产生链表头节点
    HsptRecNode* temp = NULL;
    HsptRecodeList = temp = readAHsptRecNode(fp);
    int num = 1;                                                // 用于记录已经录入的数目
    while(!feof(fp) && num++ < PatientInHsptNum)
    {
        temp->next = readAHsptRecNode(fp);
        temp->next->pre = temp;
        temp = temp->next;
    }
    temp->next = NULL;
    fclose(fp);
    return HsptRecodeList;
}

bool writeAHsptRecode(FILE* fp,HsptRecNode* patient)
{
    
    fprintf(fp,"%s ",patient->data.patientid);    // 写入id
    writeTime(fp,&(patient->data.in));          // 写入Date in
    writeTime(fp,&(patient->data.preout));      // 写入 Date preout
    fprintf(fp,"%.2f\n",patient->data.deposit);     // 写入押金
    return 1;
}

bool writeHsptRecNodeList()
{
    FILE* fp = NULL;
    if((fp = fopen("hospital.json","w")) == 0)
    {
        printf("Can't open patient.json");
        return 0;
    }
    HsptRecNode* temp = HsptRecList;
    int num = 0;
    fprintf(fp,"%d\n",PatientInHsptNum);
    // if( != 1)
    // {
    //     printf("ERROR: Fail to Write HsptRecode Num");
    //     return 0;
    // }
    while(num++ < PatientInHsptNum && temp)
    {
        if(writeAHsptRecode(fp,temp) == 0)
        {
            printf("ERROR: Fail to Write HsptRecode NO.%d",num);
            return 0;
        }
        temp = temp->next;
    }
    fclose(fp);
    return 1;
}

bool newHsptPatient(PatientNode *patient,Date in,float deposit)
{
    HsptRecNode *temp = createNullHsptRecNode();
    strcpy(temp->data.patientid, patient->data.idetify_card);
    temp->data.in = in;
    temp->data.deposit = deposit;

    HsptRecNode *p = HsptRecList;
    while(p->next)
        p = p->next;
    p->next = temp;
    temp->pre = p;

    return 1;// 此处可以更加健壮
}

bool updateIncome()
{
    HsptRecNode *temp = HsptRecList;
    while(temp)
    {
        HospitalIncome += 200;
        temp->data.deposit -= 200;
        temp = temp->next;
    }
    return 1;
}

bool updateHspt()
{
    Date now = getNowtime();
    Date origin = now;
    origin.hour = Origin;
    origin.min = 0;
    //displayTime(origin);
    if(cmpDate(LastUpdateTime,origin) < 0)
    {
        if(cmpDate(now,origin) > 0)
        {
            printf("处理中\n");
            printf("----------------------------------------------------\n");
            updateIncome();
        }
    }
    LastUpdateTime = now;

    return 1;
}

// HsptRecNode* findPatient(PatientNode *patient)
// {
//     HsptRecNode* temp = HsptRecList;
//     while(temp)
//     {
//         if(strcmp(temp->data.patientid,patient->data.idetify_card) == 0)
//             return temp;
//     }
//     printf("Fail to find patient:%s",patient->data.idetify_card);
//     return 0;
// }

//bool outHspt(PatientNode* patient)
bool outHspt(HsptRecNode *outNode)
{
    //HsptRecNode *temp = findPatient(patient);
    HsptRecNode *temp = outNode;
    // temp->pre->next = temp->next;
    // temp->next->pre = temp->pre;
    // free(temp);
    if(temp->pre)
        temp->pre->next = temp->next;
    if(temp->next)
        temp->next->pre = temp->pre;
    // 这里非常妙！！！
    //更新
    free(temp);
    PatientInHsptNum--;
    return 1;       //attention
}

int displayPreOut()
{
    int i = 0;
    HsptRecNode* temp = HsptRecList;
    while(temp)
    {
        if(temp->data.deposit < 1000)
            {
                printf("患者【%s】押金不足\n",temp->data.patientid);
                i++;
            }
        temp = temp->next;
    }
    return i;
}

bool pay(HsptRecNode *topay)
{
    float fare = 0;
    printf("当前患者【%s】的押金余额为%.2f\n",topay->data.patientid,topay->data.deposit);
    printf("请输入缴纳的金额：");
    bool i = getFloat(&fare);
    while(!i)
    {
        printf("请重新输入：");
        i = getFloat(&fare);
    }
    while(fare + topay->data.deposit < 1000)
    {
        printf("缴纳押金后余额仍然不足1000,请重新输入:");
        scanf("%f",&fare);
    }
    topay->data.deposit += fare;
    printf("缴纳成功，当前余额为：%.2f\n",topay->data.deposit);
    return 1;
}

bool outOrPay()
{
    int num = displayPreOut();
    printf("共有%d位患者押金不足\n",num);
    if(num == 0)
    {
        printf("处理完毕！\n");
        return 1;
    }
    HsptRecNode *temp = HsptRecList;
    int choice = 0;
    char id[19];
    while(temp)
    {
        if(temp->data.deposit < 1000)
        {
            strcpy(id,temp->data.patientid);
            printf("是否继续住院?（1 是，2 否）：");
 A:         scanf("%d",&choice);
            getchar();
            switch(choice)
            {
                case 1:pay(temp);printf("押金缴纳成功，继续住院\n");break;
                case 2:outHspt(temp);printf("患者【%s】出院\n",id);break;
                default:printf("输入错误，请重新输入（1 住院，2 出院）：");goto A; // 使用goto语句
            }
            printf("患者【id】处理成功\n",temp->data.patientid);    
        }
        
        temp = temp->next;
    }
    printf("处理完毕！");
    return 1;
}

void displayAllRecord()
{
    HsptRecNode* temp = HsptRecList;
    while(temp)
    {
        printf("患者【%s】：\n住院时间：%d年%d月%d日%d时%d分\n拟出院时间：%d年%d月%d日%d时%d分\n押金：%.2f元\n",
            temp->data.patientid,
            temp->data.in.year,
            temp->data.in.month,
            temp->data.in.day,
            temp->data.in.hour,
            temp->data.in.min,
            temp->data.preout.year,
            temp->data.preout.month,
            temp->data.preout.day,
            temp->data.preout.hour,
            temp->data.preout.min,
            temp->data.deposit
            );
        temp = temp->next;
    }
}